//
//  WLGenderSelectView.m
//  WanLian
//
//  Created by muzhuang on 2018/10/9.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLGenderSelectView.h"

@implementation WLGenderSelectView

- (void)showGenderSelectViewAction:(void(^)(NSInteger index))action
{
    self.action = action;
    self.frame = [[UIScreen mainScreen]bounds];
    self.backgroundColor = wlRGBACOLOR(0, 0, 0, 0.5);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    tapGender = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGenderBack)];
    tapGender.delegate = self;
    [self addGestureRecognizer:tapGender];
//    UIView *vback = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT_SCREEN_WL-WLScaleX(208), WIDTH_SCREEN_WL, WLScaleX(208))];
//    vback.backgroundColor = [UIColor whiteColor];
    CGRect rect = CGRectMake(0, HEIGHT_SCREEN_WL-WLScaleX(208), WIDTH_SCREEN_WL, WLScaleX(208));
    UITableView *tbGender = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT_SCREEN_WL, rect.size.width, rect.size.height) style:UITableViewStylePlain];
    tbGender.scrollEnabled = NO;
    tbGender.dataSource = self;
    tbGender.delegate = self;
    tbGender.rowHeight = WLScaleX(52);
    [self addSubview:tbGender];
    
    [UIView animateWithDuration:0.25 animations:^{
        tbGender.frame = rect;
    }];
}

- (void)tapGenderBack
{
    [self genderDismiss];
}

- (void)genderDismiss
{
    [self removeGestureRecognizer:tapGender];
    [self removeFromSuperview];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strCell = @"genderCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
    }
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, cell.contentView.bounds.size.height)];
    labTitle.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:labTitle];
    switch (indexPath.row) {
        case 0:
        {
            labTitle.font = [WLCommonTool fontTypePingFangSCRegular:17];
            labTitle.textColor = wlBlackColor;
            labTitle.text = @"性别";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
            break;
        case 1:
        {
            labTitle.font = [WLCommonTool fontTypePingFangSCRegular:14];
            labTitle.textColor = wlDarkGrayColor;
            labTitle.text = @"男";
        }
            break;
        case 2:
        {
            labTitle.font = [WLCommonTool fontTypePingFangSCRegular:14];
            labTitle.textColor = wlDarkGrayColor;
            labTitle.text = @"女";
        }
            break;
        case 3:
        {
            labTitle.font = [WLCommonTool fontTypePingFangSCRegular:17];
            labTitle.textColor = wlDarkGrayColor;
            labTitle.text = @"取消";
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    if (row == 3) {
        [self genderDismiss];
    }
    else if (row==1||row==2)
    {
        if (self.action) {
            self.action(row);
        }
        [self genderDismiss];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"])
    {
        return NO;
    }
    else
        return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
